package com.mmy.controller.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.oa.LeaveType;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.oa.impl.LeaveTypeServiceImpl;

/**
 * 类的介绍：请假类型
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class LeaveTypeController {

	@Autowired
	LeaveTypeServiceImpl leaveTypeServiceImpl;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-03
	 */
	@RequestMapping(value = "/leavetype")
	public String ToLeaveType(Model model) {
		
		List<LeaveType> lstLeaveType = leaveTypeServiceImpl.findAll();
		model.addAttribute("ltlist", lstLeaveType);
		return "OA/leaveandreview/leavetype";
		
	}

	@RequestMapping(value = "/createleavetype")
	public String ToCreateLeaveType() {
		return "OA/leaveandreview/createleavetype";
	}

	/**
	 * 函数功能说明： 新建请假类型
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-03
	 */

	@RequestMapping(value = "/subcreateleavetype")
	public String CreateLeaveType(LeaveType leaveType, HttpSession session) throws Exception {
//		获取最大值
		Object oTypeID = leaveTypeServiceImpl.getMaxTypeID();
		/* 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一 */
		if (oTypeID == null) {
			leaveType.setTypeID(1);

		} else {
			leaveType.setTypeID((int) oTypeID + 1);
		}
		String strTypeName = leaveType.getTypeName();

		try {

			/* 判断名称是否为空 */
			if (strTypeName == "" || strTypeName == null) {
				return "test";
			} else {
				/* 判断名称是否已存在 */
				if (leaveTypeServiceImpl.findByTypeName(strTypeName).size() > 0) {

					return "test";
				} else {

					leaveType.setCreateDate(new Date());
					TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
					leaveType.setCreator(tSysUser.getUserName());
					leaveTypeServiceImpl.save(leaveType.getTypeID());
					leaveTypeServiceImpl.save(leaveType);
					return "redirect:leavetype";

				}
			}

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除请假类型
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-04
	 */

	@PostMapping("/delleavetype")
	@ResponseBody
	public String DelLeaveType(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstTypeID = new ArrayList<>();

		for (String str : checklist) {
			lstTypeID.add(Integer.parseInt(str));
		}

		leaveTypeServiceImpl.delLeaveType(lstTypeID);

		return "OA/leaveandreview/leavetype";
	}

	/**
	 * 函数功能说明： 修改请假类型
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-03
	 */

	@RequestMapping(value = "/enterupdateleavetype")
	public String EnterUpdateLeaveType(HttpServletRequest request, Model model) {

		int iTypeID = Integer.parseInt(request.getParameter("iTypeID"));

		LeaveType leaveType = leaveTypeServiceImpl.findByTypeID(iTypeID);
		model.addAttribute("leaveType", leaveType);
		return "OA/leaveandreview/updateleavetype";

	}

	@RequestMapping(value = "/updateleavetype")
	public String UpdateLeaveType(LeaveType leaveType, HttpSession session) throws Exception {

		try {
			/* 定义一个新的实体类对象 */
			LeaveType dleaveType = leaveTypeServiceImpl.findByTypeID(leaveType.getTypeID());
			dleaveType.setMemo(leaveType.getMemo());
			dleaveType.setTypeName(leaveType.getTypeName());
			dleaveType.setPayment(leaveType.getPayment());
			dleaveType.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dleaveType.setReviser(tSysUser.getUserName());

			String strTypeName = leaveType.getTypeName();

			if (leaveTypeServiceImpl.findByTypeName(strTypeName).size() > 0) {

				return "test";
			} else {

			leaveTypeServiceImpl.save(leaveType);
			return "redirect:leavetype";

			}

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询请假类型
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期 2020-08-03
	 */
	@RequestMapping(value = "/searchleavetype")
	public String FindLeaveType(HttpServletRequest request, Model model) {

		String strTypeName = request.getParameter("TypeName");
		if (strTypeName == "") {
			List<LeaveType> lstLeaveType = leaveTypeServiceImpl.findAll();
			model.addAttribute("ltlist", lstLeaveType);
			request.setAttribute("strTypeName", strTypeName);
			return "OA/leaveandreview/leavetype";
		} else {
			List<LeaveType> lstLeaveType = leaveTypeServiceImpl.findByTypeName(strTypeName);
			model.addAttribute("ltlist", lstLeaveType);
			request.setAttribute("strTypeName", strTypeName);
			return "OA/leaveandreview/leavetype";
		}

	}

}
